﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace ParkLBS.Controller
{
    /// <summary>JSON序列化和反序列化辅助类</summary>
    public class JsonHelper
    {
        /// <summary>JSON序列化</summary>
        public static String JsonSerializer(Object objValue)
        {
            String strResult = String.Empty;

            if (objValue != null)
            {
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

                strResult = jsonSerializer.Serialize(objValue);
            }
            return strResult;
        }

        /// <summary>JSON反序列化</summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            return jsonSerializer.Deserialize<T>(jsonString);
        }


        /// <summary>
        /// 获取表单输入项值的集合(从隐藏域中解析)
        /// </summary>
        /// <param name="formValue"></param>
        /// <returns></returns>
        public static List<FormInputItem> GetFormValuesByInputItemIDs(string formValue)
        {
            List<FormInputItem> objFormOperationList = new List<FormInputItem>();

            if (!String.IsNullOrEmpty(formValue))
            {
                JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();

                objFormOperationList = JsonDeserialize<List<FormInputItem>>(formValue);
            }

            return objFormOperationList;
        }
    }

    #region json class

    /// <summary>
    /// 表单控件类（用于表单控件取值和赋值）
    /// </summary>
    //[DataContract]
    [Serializable]
    public class FormInputItem
    {
        /// <summary>
        /// html控件名称
        /// </summary>
        public String name { get; set; }

        /// <summary>
        /// html控件值
        /// </summary>
        public String value { get; set; }
    }

    #endregion
}
